package com.jams.goodox.activity.qa.teacher;

import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.view.View;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.activity.qa.entity.Question;
import com.jams.goodox.adapter.lfadapter.CommonRecycleViewAdapter;
import com.jams.goodox.adapter.lfadapter.LFRecyclerViewHolder;
import com.jams.goodox.utils.TimeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import me.leefeng.lfrecyclerview.LFRecyclerView;
import me.leefeng.lfrecyclerview.OnItemClickListener;

/**
 * Created by HP on 2018/5/8.
 *
 * 问题列表
 * api/question/own
 */

public class QuestionListActivity extends BaseActivity implements
        OnItemClickListener,LFRecyclerView.LFRecyclerViewListener,
        LFRecyclerView.LFRecyclerViewScrollChange {

    @Bind(R.id.recycler_view)
    LFRecyclerView recyclerView;

    private int page = 0;
    private CommonRecycleViewAdapter pAdapter;
    private Handler mHandler = new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            if(msg.what ==1){
                requestData();
            }
        }
    };
    private List<Question> list = new ArrayList<>();


    private boolean status = false;//判断加载成功还是失败
    private boolean isRefresh = true;
    private int pageNum = 1;


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tacherqa;
    }

    @Override
    protected void init() {

        mHandler.sendEmptyMessageDelayed(1,1000);

        recyclerView.setLoadMore(true);
        recyclerView.setRefresh(true);
        recyclerView.setNoDateShow();
        recyclerView.setAutoLoadMore(true);
        recyclerView.setOnItemClickListener(this);
        recyclerView.setLFRecyclerViewListener(this);
        recyclerView.setScrollChangeListener(this);
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        pAdapter = new CommonRecycleViewAdapter(recyclerView,list,R.layout.question_item) {
            @Override
            public void bindConvert(LFRecyclerViewHolder holder, int position, Object item, boolean isScrolling) {

                Question question = (Question) item;
                holder.setText(R.id.nickName,question.getNickName());
                holder.loadImage(R.id.title,question.getTitle());
                holder.setText(R.id.createTime, TimeUtils.getDateToString(question.getCreateTime()));
            }


        };

        recyclerView.setFootText("加载更多");

        recyclerView.setAdapter(pAdapter);
    }




    public void requestData() {

        //list.addAll(Arrays.asList(tt));
        pAdapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(int position) {

    }

    @Override
    public void onLongClick(int po) {

    }

    @Override
    public void onRecyclerViewScrollChange(View view, int i, int i1) {

    }

    @Override
    public void onRefresh() {
        pageNum = 1;
        isRefresh = true;

        //requestData();
    }

    @Override
    public void onLoadMore() {
        pageNum ++;
        isRefresh = false;
        //requestData();
    }
}
